package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.entity.SpuSaleAttrValue;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SpuInfoSaveVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 李天玉
 * @description 针对表【spu_info(商品表)】的数据库操作Service实现
 * @createDate 2025-08-15 14:26:51
 */
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
        implements SpuInfoService {
    @Autowired
    private SpuImageService spuImageService;

    @Autowired
    private SpuSaleAttrService spuSaleAttrService;

    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;

    /**
     * 分页获取SPU列表
     *
     * @param category3Id
     * @param pn
     * @param ps
     * @return
     */
    @Override
    public Page<SpuInfo> getSpuInfoPage(Long category3Id, Long pn, Long ps) {
        Page<SpuInfo> page = new Page<>(pn, ps);
        LambdaQueryWrapper<SpuInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SpuInfo::getCategory3Id, category3Id);
        Page<SpuInfo> result = page(page, wrapper);
        return result;
    }

    /**
     * 保存SPU信息
     *
     * @param spuInfoSaveVo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveSpuInfo(SpuInfoSaveVo spuInfoSaveVo) {
        //1.保存SPU信息
        SpuInfo spuInfo = new SpuInfo();
        //复制属性字段值
        BeanUtils.copyProperties(spuInfoSaveVo, spuInfo);
        this.save(spuInfo);
        //获取spuId
        Long id = spuInfo.getId();

        //2.保存图片信息
        List<SpuImage> images = spuInfoSaveVo.getSpuImageList().stream()
                .map(spuImage -> {
                    SpuImage image = new SpuImage();
                    //属性拷贝
                    BeanUtils.copyProperties(spuImage, image);
                    //设置spuId
                    image.setSpuId(id);
                    return image;
                }).collect(Collectors.toList());
        spuImageService.saveBatch(images);

        //3、保存销售属性名spu_sale_attr List<Spusaleattrlist>
        spuInfoSaveVo.getSpuSaleAttrList().stream()
                .forEach(item -> {
                    SpuSaleAttr spuSaleAttr = new SpuSaleAttr();
                    BeanUtils.copyProperties(item, spuSaleAttr);
                    spuSaleAttr.setSpuId(id);
                    //保存销售属性名
                    spuSaleAttrService.save(spuSaleAttr);

                    //拿到销售属性值；List<Spusaleattrvaluelist>
                    List<SpuSaleAttrValue> attrValueList = item.getSpuSaleAttrValueList().stream()
                            .map(val -> {
                                SpuSaleAttrValue attrValue = new SpuSaleAttrValue();
                                BeanUtils.copyProperties(val, attrValue);
                                //回填spuId
                                attrValue.setSpuId(id);
                                attrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                                return attrValue;
                            }).collect(Collectors.toList());
                    //保存销售属性值
                    spuSaleAttrValueService.saveBatch(attrValueList);
                });
    }
}




